package com.cd2cd.code_template.code_template.mapper;

import com.cd2cd.code_template.code_template.domain.SysAuth;
import com.cd2cd.code_template.code_template.domain.SysUser;
import com.cd2cd.code_template.code_template.mapper.gen.SuperSysUserMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysUserMapper extends SuperSysUserMapper {

    @Update("update sys_user set del_flag=#{delFlag} where id=#{id}")
    Integer updateDelFlagById(@Param("id") Integer id, @Param("delFlag") Integer delFlag);

    @Select("select * from sys_user where username=#{username} and status='enable' and del_flag='ok' limit 1")
    @ResultMap("BaseResultMap")
    SysUser findUserByUsername(@Param("username") String username);

    @Select("select " +
            "* " +
            "from " +
            "sys_auth " +
            "where guid in " +
            "(" +
            "select " +
            "auth_guid " +
            "from " +
            "sys_relation_auth where `type`='role' and login_type=#{type} and relation_id in " +
            "(select relation_id from sys_relation_user where `type`='role' and login_type=#{type} and user_id = #{userId}) " +
            "union " +
            "select " +
            "auth_guid " +
            "from " +
            "sys_relation_auth where `type`='group' and login_type=#{type} and relation_id in " +
            "(select relation_id from sys_relation_user where `type`='group' and login_type=#{type} and user_id = #{userId}) " +
            ");")
    @ResultType(SysAuth.class)
    List<SysAuth> queryUserAuth(@Param("userId") Integer userId, @Param("type")String type);

    @Select("select relation_id from sys_relation_user where user_id = #{userId} and type = #{type}")
    @ResultType(Integer.class)
    List<Integer> selectRelation(@Param("userId") Integer userId,@Param("type") String type);
}